package com.prototype.mapping {
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author Santiago.J.Elizalde
	 */
	public class Map {
		
		private var types:Array;
		private var blockSize:int = 2;
		private var blockInterfaceSize:int = 3;
		
		private var library:TileLibrary;
		
		public function Map() {
			
			library = new TileLibrary();
			
			types = [];
			types.push("flat")
			types.push("flat_with_platform")
			types.push("flat_with_platform")
			types.push("flat_with_block")
			types.push("water_hole")
			types.push("water_hole_with_platform")
			types.push("water_hole_with_platform")
		}
		
		public function buildBlock():Array {
			
			var type:String = types[Math.floor(Math.random() * types.length)]
			var block:Array;
			switch(type) {
				case "flat":
					block = createFlatBlock();
					break;
				case "flat_with_platform":
					block = createPlatformBlock();
					break;
				case "flat_with_block":
					block = createFlatBlockedBlock();
					break;
				case "water_hole":
					block = createWaterBlock();
					break;
				case "water_hole_with_platform":
					block = createWaterWithPlatBlock();
					break;
			}
			block = block.concat(createInterface());
			return block;
		}
		
		private function createWaterWithPlatBlock():Array {
			var block:Array = [];
			
			block.push(library.getTemplateLine(TileLibrary.WATER_END_LEFT));
			block.push(library.getTemplateLine(TileLibrary.WATER_PLATFORM_BEGIN));
			for (var i:int=0; i < blockSize; i++) {
				block.push(library.getTemplateLine(TileLibrary.WATER_PLATFORM_PIECE));
			}
			block.push(library.getTemplateLine(TileLibrary.WATER_PLATFORM_END));
			block.push(library.getTemplateLine(TileLibrary.WATER_END_RIGHT));
			return block;
		}
		
		private function createWaterBlock():Array {
			var block:Array = [];
			
			block.push(library.getTemplateLine(TileLibrary.WATER_END_LEFT));
			for (var i:int=0; i < blockSize; i++) {
				block.push(library.getTemplateLine(TileLibrary.WATER_GAP));
			}
			block.push(library.getTemplateLine(TileLibrary.WATER_END_RIGHT));
			return block;
		}
		
		private function createFlatBlockedBlock():Array {
			var block:Array = [];
			
			for (var i:int = 0; i < blockSize; i++) {
				block.push(library.getTemplateLine(TileLibrary.BLOCKED));
			}
			return block;
		}
		
		private function createPlatformBlock():Array {
			var block:Array = [];
			
			block.push(library.getTemplateLine(TileLibrary.PLATFORM_BEGIN));
			for (var i:int = 0; i < blockSize; i++) {
				block.push(library.getTemplateLine(TileLibrary.PLATFORM_PIECE));
			}
			block.push(library.getTemplateLine(TileLibrary.PLATFORM_END));
			return block;
		}
		
		private function createFlatBlock():Array {
			var block:Array = [];
			for (var i:int = 0; i < blockSize; i++) {
				block.push(library.getTemplateLine(TileLibrary.FLAT));
			}
			return block;
		}
		
		private function createInterface():Array {
			var interfaceBlock:Array = [];
			for (var i:int = 0; i < blockInterfaceSize; i++) {
				interfaceBlock.push(library.getTemplateLine(TileLibrary.FLAT));
			}
			return interfaceBlock;
		}
	}
}